package com.auto.finance.auth.collect.support.handles;

import com.auto.finance.auth.collect.annotations.ApiInclude;
import com.auto.finance.auth.collect.beans.ApiInfo;
import com.auto.finance.auth.collect.support.RequestPathHandle;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author tianye
 */
public abstract class AbstractRequestPathHandle implements RequestPathHandle {

    @Override
    public List<ApiInfo> handle(String[] prefix, Annotation annotation, ApiInclude apiInclude) {

        List<ApiInfo> entities = new ArrayList<>();

        List<String> pathList;
        if (null != prefix && prefix.length > 0) {
            pathList = build(prefix, getPaths(annotation));
        } else {
            String[] paths = getPaths(annotation);
            pathList = paths.length > 0 ? Arrays.asList(paths[0]) : Collections.emptyList();
        }

        if (pathList.size() == 0) {
            return Collections.emptyList();
        }

        /**
         * 此处为方便以后修改，使用了list的返回值支持对多url的支持;
         * 但目前通过判断保证了此处最多只有一条记录
         */
        for (String s : pathList) {
            if (!s.startsWith("/")) {
                s = "/" + s;
            }
            String formatPath = s
                    .replaceAll("/", "::")
                    .replaceAll("\\{", "#")
                    .replaceAll("\\}", "#");
            for (String requestMethod : getMethod(annotation)) {
                String name = null;
                String apiId = null;
                String menuId = null;
                if (null != apiInclude) {
                    if (apiInclude.name().length() > 0) {
                        name = apiInclude.name();
                    }
                    if (apiInclude.apiId().length() > 0) {
                        apiId = apiInclude.apiId();
                    } else {
                        apiId = String.format("%s::%s", formatPath, requestMethod.toLowerCase());
                    }
                    if (apiInclude.menuId().length() > 0) {
                        menuId = apiInclude.menuId();
                    }
                } else {
                    apiId = String.format("%s::%s", formatPath, requestMethod.toLowerCase());
                }

                entities.add(
                        new ApiInfo()
                                .setPath(s)
                                .setRequestMethod(requestMethod)
                                .setName(name)
                                .setApiId(apiId)
                                .setMenuId(menuId)
                );
            }
        }

        return entities;
    }

    /**
     * 从注解中获取path
     *
     * @param annotation
     * @return
     */
    protected abstract String[] getPaths(Annotation annotation);

    /**
     * 从注解中获取请求方法
     *
     * @param annotation
     * @return
     */
    protected abstract String[] getMethod(Annotation annotation);
}
